!function () {
    function request(url,method,data,success){
        $$.ajax({
            method:method,
            url:url,
            data:data,
            dataType:'json',
            success:success
        });
    }
    function checkUserComplished(url)
    {
        $$.ajax({
            method:'GET',
            url:url,
            data:{},
            dataType:'json',
            xhrFields:{"withCredentials":true},
            success:function (res) {
                // console.log(res);
                if (res.code==1){
                    Globaluid=parseInt(res.data.uid);
                    //用户已经填写
                    if (res.data.status==2) {
                        mainView.router.load({"url":"result.html"});
                    }else{
                        Globalgnum = res.data.info.length;
                        var html = Pointapp.groupItemTemplate(res.data.info);
                        $$('#Point .point-content-list').html(html);
                    }
                }else{window.location.href="http://api.nldou.com/ucenter/weixinlogin?pid=2426";}
            }
        });
    }

    var $$=Dom7;
    var Globalans=[];
    var Globaluid;
    var Globalgnum;

    var Initjssdk_url = "http://api.nldou.com/weixinapi/init_wx_jsapi";
    var Check_complished_url = "http://api.nldou.com/point/checkUserComplish";
    var Submit_ans_url = "http://api.nldou.com/point/submitAns";

    $$.ajax({
        url:Initjssdk_url,
        method:"GET",
        data: {fromUrl:window.location.href},
        dataType:'json',
        success: function (res) {
            if(res.code==1){
                wx.config({
                    debug: false,
                    appId: res.data.appid,
                    timestamp: res.data.timestamp,
                    nonceStr: res.data.nonceStr,
                    signature: res.data.signature,
                    jsApiList: ['hideMenuItems']
                });
            }
        }
    });

    wx.ready(function () {
        wx.hideMenuItems({
            menuList: ['menuItem:share:appMessage','menuItem:share:timeline','menuItem:share:qq'
                ,'menuItem:share:weiboApp', 'menuItem:share:facebook', 'menuItem:share:QZone'
                ,'menuItem:copyUrl', 'menuItem:openWithQQBrowser', 'menuItem:openWithSafari']
        });

    });

    /****Initialize your app****/
    var Pointapp = new Framework7({
        animateNavBackIcon: true,
        modalTitle:'启发式提问',
        modalButtonOk:'确认',
        modalButtonCancel:'取消',
        swipeBackPage:false
    });

    /****Add view****/
    var mainView = Pointapp.addView('.view-main', {
        dynamicNavbar: true
    });

    Pointapp.groupItemTemplate = Template7.compile($$('script#Groupitem').html());

    checkUserComplished(Check_complished_url);

    $$(document).on('click','.btn',function () {
        var val=parseInt($$(this).data('val'));
        //是否重复
        var repeat = false;
        for (var i in Globalans){
            if (Globalans[i]===val){
                i = parseInt(i)+1;
                Pointapp.alert('打分不可与第'+i+'组重复，请重新选择');
                repeat = true;
                break;
            }
        }
        if (!repeat) {
            //存储答案
            var gid=parseInt($$(this).data('gid'))-1;
            Globalans[gid]=val;
            //选中选项
            var box=$$(this).parent().parent();
            box.find('.btn').removeClass('active');
            $$(this).addClass('active');
        }
        // console.log(Globalans);
    });
    $$(document).on('click','.submit-btn',function () {
        Pointapp.showPreloader('提交中');
        if ($$('.btn.active').length==Globalgnum) {
            Pointapp.hidePreloader();
            console.log(Globaluid,Globalans);
            request(Submit_ans_url,'POST',{uid:Globaluid,ans:Globalans},function (res) {
                console.log(res.data);
                if (res.code==1){
                    Pointapp.hidePreloader();
                    mainView.router.load({"url":"result.html"});
                }else{
                    Pointapp.hidePreloader();
                    Pointapp.alert('系统繁忙请重试','');
                }
            });
        }else{
            Pointapp.hidePreloader();
            Pointapp.alert('请完成所有组打分','');
        }
    });
}();
